/**
*add by cjj 销售品保存
*/
import OrderService from "@/api/order/OrderService.js";
const prodSave = {
    buildObject: (res, agentList, busiManagerId, custManagerId, ppmParams) => {
        // let prodSaveInfo = {}; //存放接口报文
        // let contractRoot = {};
        // let svcCont = {};
        let requestObject = {};
        requestObject.custOrderId = res.custOrderId;
        requestObject.custSoNumber = res.custSoNumber;
        requestObject.operType = res.operType;
        requestObject.custOrderType = res.custOrderType;
        requestObject.orderSourceId = res.orderSourceId;
        requestObject.createStaff = res.createStaff;
        requestObject.createOrgId = res.createOrgId;
        requestObject.accepteRegionId = res.accepteRegionId;
        requestObject.accepteLanId = res.accepteLanId;
        requestObject.custId = res.custId;
        requestObject.custName = res.custName;
        requestObject.sysSource = res.sysSource;
        requestObject.channelId = res.channelId;
        requestObject.orderSource = res.orderSource;
        requestObject.bussDesc = res.bussDesc;
        requestObject.remark = res.remark;
        requestObject.contactId = res.contactId;
        requestObject.contactCd = res.contactCd;
        requestObject.contactName = res.contactName;
        requestObject.contractNumberType = res.contractNumberType;
        requestObject.projectId = res.projectId;
        requestObject.projectName = res.projectName;
        requestObject.projectManage = res.projectManage;
        requestObject.saleId = res.saleId;
        requestObject.extCustOrderId = res.extCustOrderId;
        var buildHandle = prodSave.buildHandle(agentList);//经办人信息
        if (Object.keys(buildHandle).length > 0) {
            requestObject.order_Handler = buildHandle;
        }
        requestObject.mainOfferInsts = prodSave.buildMainOffer(res.result, busiManagerId, custManagerId, ppmParams, res.respCustId);//主销售品
        // svcCont.requestObject = requestObject;
        // contractRoot.svcCont = svcCont;
        // prodSaveInfo.contractRoot = contractRoot;
        return requestObject;
    },

    buildHandle: (res) => {//经办人信息
        let order_Handler = {}; //经办人信息
        res.forEach((item) => {
            let tableName = item.tableName;
            let colId = item.colId;
            let value = item.value;
            if (tableName == "ORDER_HANDLER") {
                order_Handler[colId] = value;
            }
        });
        return order_Handler;
    },

    buildMainOffer: (res, busiManagerId, custManagerId, ppmParams, respCustId) => {//主销售品
        var mainOfferInsts = [];
        res.forEach((item) => {
            var obj = prodSave.buildMainOfferObj(item, busiManagerId, custManagerId, ppmParams, respCustId);
            mainOfferInsts.push(obj)
        })
        return mainOfferInsts;
    },

    buildMainOfferObj: (res, busiManagerId, custManagerId, ppmParams, respCustId) => {//销售品分支
        var obj = {};
        obj.offerId = ppmParams.offerId;//销售品标识 前一页面传回
        obj.serviceOfferId = ppmParams.serviceOfferId;//操作id", 新装4010100000
        if (JSON.parse(sessionStorage.getItem("FormModel")).orderstage) {//从商机进
            obj.handleStateCd = JSON.parse(sessionStorage.getItem("FormModel")).orderstage || "";
        }
        res.forEach((item) => {//查询ORDER_ITEM
            if (item.tableName == 'ORDER_ITEM') {
                if (item.colId == 'orderDate') {//要求完成时间需求单用reqOrderDate，订单用orderDate
                    if (JSON.parse(sessionStorage.getItem("FormModel")).orderstage) {//从商机进
                        if (JSON.parse(sessionStorage.getItem("FormModel")).orderstage == 50030003) {//订单起
                            item.colId = 'orderDate'
                        } else {//需求单起
                            item.colId = 'reqOrderDate'
                        }
                    } else { //无起单阶段就都传
                        item.colId = 'reqOrderDate';
                        obj["orderDate"] = item.value;
                    }
                }
                obj[item.colId] = item.value;
            }
        })
        obj.orderItemAttrs = prodSave.buildItemAttr(res);//查询ORDER_ITEM_ATTR
        obj.orderContactInfos = prodSave.buildContactInfos(res);//查询甲乙端信息ORDER_CONTACT_INFO  2.0表名PARTY_CONTACT_INDO
        obj.orderItemFiles = prodSave.buildItemFiles(res);//附件ORDER_ITEM_FILE
        obj.ordDevStaffInfos = prodSave.orderDevStaffInfos(res);//发展人
        obj.acctItemOrgs = prodSave.buildItemOrgs(res);//一次费ACCT_ITEM_ORG
        obj.ordProdInsts = prodSave.buildProdInst(res, busiManagerId, custManagerId, ppmParams, respCustId);//产品实例ORD_PROD_INST

        return obj;
    },

    buildItemAttr: (res) => {
        var orderItemAttrs = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_ITEM_ATTR') {
                obj.attrId = res.colId;
                obj.attrValue = res.value;
                obj.attrCd = res.fieldId;
                orderItemAttrs.push(obj);
            }
        })
        return orderItemAttrs;
    },

    buildContactInfos: (res) => {//甲乙端信息
        var orderContactInfos = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_CONTACT_INFO') {
                if (res.attrRoleCd == '100') { obj.contactType = '6'; } if (res.attrRoleCd == '200') { obj.contactType = '7'; }
                obj[res.colId] = res.value;
                orderContactInfos.push(obj);
            }
        })
        let orderContactInfo = prodSave.mergeObject(orderContactInfos, 'contactType')
        return orderContactInfo;
    },

    buildItemFiles: (res) => {//附件
        let orderItemFiles = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORDER_ITEM_FILE') {
                obj[res.colId] = res.value;
                orderItemFiles.push(obj);
            }
        })
        let orderItemFile = prodSave.mergeObject(orderItemFiles, '')
        return orderItemFile;
    },

    orderDevStaffInfos: (res) => {//发展人
        let ordDevStaffInfos = [];
        res.forEach((res) => {
            var obj = {};
            if (res.tableName == 'ORD_DEV_STAFF_INFO' && res.value != '') {
                obj.devStaffType = res.devStaffType;
                obj.remark = "";//暂时不需要传值
                obj[res.colId] = res.value;
                ordDevStaffInfos.push(obj);
            }
        })
        let ordDevStaffInfo = prodSave.mergeObject(ordDevStaffInfos, 'devStaffType')
        return ordDevStaffInfo;
    },

    buildItemOrgs: (res) => {//资费信息
        let acctItemOrgs = [];
        res.forEach((item) => {
            let newObj = res.filter((item) => {
                return item.tableName == 'ACCT_ITEM_ORG' && (item.attrRoleCd == '0' || !item.attrRoleCd)
            })

            if (item.tableName == 'ACCT_ITEM_ORG') {
                var obj = {};
                // obj.chargeOrgCd = item.attrRoleCd;
                // obj[item.colId] = item.value;
                // if (item.attrRoleCd == '0' || !item.attrRoleCd) {
                //     let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                //     obj.acctItems = acctItems;
                // }
                if (item.attrRoleCd == '100') {
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }
                    obj.chargeOrgCd = item.attrRoleCd;
                    obj[item.colId] = item.value;
                    let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                    obj.acctItems = acctItems;
                    acctItemOrgs.push(obj);
                }
                if (item.attrRoleCd == '200') {
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }
                    obj.chargeOrgCd = item.attrRoleCd;
                    obj[item.colId] = item.value;
                    let acctItems = prodSave.buildAcctItem(res, item.attrRoleCd);//一次费帐目类型标识分组ACCT_ITEM
                    obj.acctItems = acctItems;
                    acctItemOrgs.push(obj);
                }

            }
        })
        let acctItemOrg = prodSave.mergeObject(acctItemOrgs, 'chargeOrgCd');
        return acctItemOrg;
    },



    buildAcctItem: (res, attrRoleCd) => {//一次费帐目类型标识分组
        let acctItems = [];
        let newArr = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId !== 'amount'
        })

        let newObj = res.filter((item) => {//0端acctitem表平分到甲乙端
            return item.tableName == 'ACCT_ITEM' && (item.attrRoleCd == '0' || !item.attrRoleCd)
        })

        let newMount = res.filter((item) => {
            return item.tableName == 'ACCT_ITEM' && item.colId == 'amount'
        })

        newMount.forEach((item) => {
            let mapArr = [];
            let obj = {};
            if (item.attrRoleCd == attrRoleCd) {
                if (item.relIds) {
                    mapArr = item.relIds.split(";")
                }
                for (let i = 0; i < newObj.length; i++) {
                    obj[newObj[i].colId] = newObj[i].value
                }
                obj.acctItemTypeId = item.acctItemId;
                obj.amount = item.value;
                obj.acctTypeCd = item.attrRoleCd == item.attrRoleCd ? item.attrRoleCd : '0';
                acctItems = prodSave.getArr(mapArr, newArr, obj, acctItems, item.attrRoleCd);
            }
        })

        var acctItem = prodSave.mergeObject(acctItems, 'acctItemTypeId');
        return acctItem;
    },

    getArr: (mapArr, newArr, obj, acctItems, attrRoleCd) => {
        if (mapArr.length > 0) {
            for (var i = 0; i < mapArr.length; i++) {
                let obj1 = {};
                for (var j = 0; j < newArr.length; j++) {
                    let id = newArr[j].id;
                    if (mapArr[i].includes(id)) {
                        if (newArr[j].attrRoleCd == attrRoleCd) {
                            obj1[newArr[j].colId] = newArr[j].value;
                        }
                    }
                }
                var obj2 = { ...obj, ...obj1 }
                acctItems.push(obj2);
            }
        } else {
            acctItems.push(obj);
        }
        return acctItems;
    },

    buildProdInst(res, busiManagerId, custManagerId, ppmParams, respCustId) {//产品实例
        let ordProdInsts = [];
        let newObj = res.filter((item) => {//20210727需求payObjId跟payType 甲乙端都需要
            return item.tableName == 'ORD_PROD_INST' && item.colId == 'payObjId' || item.colId == 'payType'
        })
        res.forEach(async (item) => {
            let obj = {};
            if (item.tableName == 'ORD_PROD_INST') {
                if (item.attrRoleCd == '100') {
                    obj.prodId = ppmParams.zProdId||'';
                    obj.prodOfferId = ppmParams.offerId;//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '100';
                    obj.paymentModeCd = '1200';
                    obj.accNum = '';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = respCustId;//存的省客户ID前一个页面带过来
                    obj[item.colId] = item.value;
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }
                }
                if (item.attrRoleCd == '200') {
                    obj.prodId = ppmParams.zProdId||'';
                    obj.prodOfferId = ppmParams.offerId;//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '200';
                    obj.paymentModeCd = '1200';
                    obj.accNum = '';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = respCustId;//存的省客户ID前一个页面带过来
                    obj[item.colId] = item.value;
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }
                }
                if (item.attrRoleCd == '0' || !item.attrRoleCd) {
                    item.attrRoleCd = '0';
                    obj.prodId = ppmParams.prodId||'';
                    obj.prodOfferId = ppmParams.offerId;//确认是否换成offerId//销售品ID需要前一个页面选择销售品的时候带过来
                    obj.roleCd = '0';
                    obj.paymentModeCd = '1200';
                    obj.accNum = '';//临时添加跟中台调试用  2021/4/23
                    obj.custManagerId = custManagerId;//前一个页面选择带过来
                    obj.busiManagerId = busiManagerId;//前一个页面选择带过来
                    obj.respCustId = respCustId;//存的省客户ID前一个页面带过来
                    obj[item.colId] = item.value;
                }
                obj.ordProdInstAttrs = prodSave.buildProdInstAttr(res, item.attrRoleCd, 'ORD_PROD_INST_ATTR');//ORD_PROD_INST_ATTR


                obj.ordProdInstAcctRels = prodSave.buildInstAcctRel(res, item.attrRoleCd);//PROD_INST_ACCT取页面表ORD_PROD_INST_ACCT_REL



                obj.ordOfferInstFeeInfos = prodSave.buildInstFeeInfo(res, item.attrRoleCd);//PROD_OFFER_FEE_INFO取页面表ORD_OFFER_INST_FEE_INFO
                ordProdInsts.push(obj);
            }
        });
        var ordProdInst = prodSave.mergeObject(ordProdInsts, 'roleCd');
        return ordProdInst;
    },

    buildProdInstAttr: (res, RoleCd, tabName) => {
        let ordProdInstAttrs = [];
        res.forEach((item) => {
            if (!item.attrRoleCd) { item.attrRoleCd = '0'; }
            if (item.attrRoleCd == RoleCd) {
                let obj = {};
                if (item.tableName == tabName) {
                    if (item.colId == '142002036') {//生效时间
                        item.value = JSON.parse(sessionStorage.getItem("FormModel")).signdate;
                    }
                    if (item.colId == '142002037') {//终止时间
                        item.value = JSON.parse(sessionStorage.getItem("FormModel")).enddate;
                    }
                    obj.attrId = item.colId;
                    obj.attrValue = item.value;
                    obj.attrCd = item.fieldId;
                    ordProdInstAttrs.push(obj)
                }
            }
        })
        return ordProdInstAttrs;
    },


    buildInstFeeInfo: (res, RoleCd) => {
        let ordOfferInstFeeInfos = [];
        let newArr = res.filter((item) => {
            return item.tableName == 'ORD_OFFER_INST_FEE_INFO' && item.colId !== 'amount'
        })

        let newMount = res.filter((item) => {
            return item.tableName == 'ORD_OFFER_INST_FEE_INFO' && item.colId == 'amount'
        })

        let newObj = res.filter((item) => {//20210727需求isFixedPrice 甲乙端都需要
            return item.tableName == 'ORD_OFFER_INST_FEE_INFO' && item.colId == 'isFixedPrice'
        })

        newMount.forEach((item) => {
            let mapArr = [];
            let obj = {};
            if (!item.attrRoleCd) { item.attrRoleCd = '0'; }
            if (item.attrRoleCd == RoleCd) {
                if (item.relIds) {
                    mapArr = item.relIds.split(";")
                }
                obj.acctItemTypeId = item.acctItemId;
                obj.acctItemGroupId = item.acctItemId;
                res.forEach((item) => {
                    if (item.tableName == 'ORD_PROD_INST' && item.colId == 'accountId') {
                        obj.acctId = item.value
                    }
                })
                obj.amount = item.value;
                obj.roleCd = item.attrRoleCd;
                if (item.attrRoleCd == '100' || item.attrRoleCd == '200') {
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }
                }
                ordOfferInstFeeInfos = prodSave.getArr(mapArr, newArr, obj, ordOfferInstFeeInfos, item.attrRoleCd);
            }
        })
        //临时处理ORD_OFFER_INST_FEE_ATTR表里数据随意挂到一个费用里就可以  这个需要跟中台定下这个节点最好就不要放在费用里面这个表里信息是跟销售品实例挂了关系 没有费用的关系
        let attrCount = 0;
        ordOfferInstFeeInfos.forEach((o) => {
            if (o.roleCd == '0' && attrCount == 0) {
                o.ordOfferInstFeeAttrs = prodSave.buildProdInstAttr(res, RoleCd, 'ORD_OFFER_INST_FEE_ATTR');//PROD_OFFER_FEE_ATTR
                attrCount++;
            }
        });

        var ordOfferInstFeeInfo = prodSave.mergeObject(ordOfferInstFeeInfos, 'acctItemTypeId');
        return ordOfferInstFeeInfo;
    },

    buildInstAcctRel: (res, RoleCd) => {
        let ordOfferInstAcctRels = [];
        let newArr = res.filter((item) => {
            return item.tableName == 'ORD_PROD_INST_ACCT_REL' && item.colId !== 'amount'
        })

        let newMount = res.filter((item) => {
            return item.tableName == 'ORD_OFFER_INST_FEE_INFO' && item.colId == 'amount'
        })

        let newObj = res.filter((item) => {//20210727需求ordProdInstAcctRels 节点roleCd100，200端增加"discountFlag" "payCycle" "groupPayMethod"  
            return item.tableName == 'ORD_PROD_INST_ACCT_REL' && item.colId == 'discountFlag' || item.colId == 'payCycle' || item.colId == 'groupPayMethod' || item.colId == 'extAcctId' || item.colId == 'extAcctCode'
        })

        newMount.forEach((item) => {
            let mapArr = [];
            let obj = {}
            if (!item.attrRoleCd) { item.attrRoleCd = '0'; }
            if (RoleCd == '100' || RoleCd == '200') {
                if (item.acctItemId == '3003000' && item.attrRoleCd == '0') {
                    if (item.relIds) {
                        mapArr = item.relIds.split(";")
                    }
                    obj.acctItemGroupId = item.acctItemId;
                    res.forEach((item) => {
                        if (item.tableName == 'ORD_PROD_INST' && item.colId == 'accountId') {
                            obj.acctId = item.value
                        }
                    })
                    for (let i = 0; i < newObj.length; i++) {
                        obj[newObj[i].colId] = newObj[i].value
                    }

                    obj.roleCd = RoleCd;
                    obj.paymentLimit = '50';
                    ordOfferInstAcctRels = prodSave.getArr(mapArr, newArr, obj, ordOfferInstAcctRels, item.attrRoleCd);
                }
            }
            if (item.attrRoleCd == RoleCd && item.acctItemId != '3003000') {
                if (item.relIds) {
                    mapArr = item.relIds.split(";")
                }
                obj.acctItemGroupId = item.acctItemId;
                res.forEach((item) => {
                    if (item.tableName == 'ORD_PROD_INST' && item.colId == 'accountId') {
                        obj.acctId = item.value
                    }
                })
                for (let i = 0; i < newObj.length; i++) {
                    obj[newObj[i].colId] = newObj[i].value
                }

                obj.roleCd = item.attrRoleCd;
                obj.paymentLimit = '100';
                ordOfferInstAcctRels = prodSave.getArr(mapArr, newArr, obj, ordOfferInstAcctRels, item.attrRoleCd);
            }
        })

        var ordOfferInstFeeInfo = prodSave.mergeObject(ordOfferInstAcctRels, 'acctItemGroupId');
        return ordOfferInstFeeInfo;
    },

    mergeObject: (arr, key) => {//将数组中特定属性相同的对象合并
        var intype = {};
        var newJson = [];
        arr.forEach((item) => {
            if (intype[item[key]]) {
                Object.assign(newJson[intype[item[key]] - 1], item);
            } else {
                newJson.push(item);
                intype[item[key]] = newJson.length;
            }
        });
        return newJson;
    },

    toHump: (name) => {
        //驼峰转换
        let key = name.toLowerCase();
        return key.replace(/_(\w)/g, function (all, letter) {
            return letter.toUpperCase();
        });
    },
};

export default prodSave;
